<?php

class Application_Model_SubscribeMapper
{
	protected $_dbTable;
	
	public function setDbTable($dbTable)
	{
		if (is_string($dbTable)) {
			$dbTable = new $dbTable();
		}
		if (!$dbTable instanceof Zend_Db_Table_Abstract) {
			throw new Exception('Invalid table data gateway provided');
		}
		$this->_dbTable = $dbTable;
		return $this;
	}
	
	public function getDbTable()
	{
		if (null === $this->_dbTable) {
			$this->setDbTable('Application_Model_DbTable_Subscribe');
		}
		return $this->_dbTable;
	}
	
	public function save(Application_Model_Subscribe $subscribe)
	{
		$data = array(
				'sender_id' => $subscribe->getSenderId(),
				'sender_addr' => $subscribe->getSenderAddr(),
				'recipient' => $subscribe->getRecipient(),
				'rec_tel' => $subscribe->getRecTel(),
				'rec_addr' => $subscribe->getRecAddr(),
				'barcodeid' => $subscribe->getBarcodeId(),
				'send_unitid' => $subscribe->getSendUnitId(),
				'rec_unitid' => $subscribe->getRecUnitId(),
				'courierid' => $subscribe->getCourierId(),
				'if_pay_now' => $subscribe->getIfPayNow(),
				'pay_type' => $subscribe->getPayType(),
				'fee' => $subscribe->getFee(),
				//'created' => date('Y-m-d H:i:s'),
		);
	
		if (null === ($id = $subscribe->getId())) {
			unset($data['id']);
			$this->getDbTable()->insert($data);
		} else {
			$this->getDbTable()->update($data, array('id = ?' => $id));
		}
	}
	
	public function find($id, Application_Model_Subscribe $subscribe)
	{
		$result = $this->getDbTable()->find($id);
		if (0 == count($result)) {
			return;
		}
		$row = $result->current();
		$subscribe->setId($row->id)
			->setSenderName($row->sender_name)
			->setSenderId($row->sender_id)
			->setSenderTel($row->sender_tel)
			->setSenderAddrId($row->sender_addrid)
			->setRecipient($row->recipient)
			->setRecTel($row->rec_tel)
			->setRecEmail($row->rec_email)
			->setRecAddrId($row->rec_addrid)
			->setBarcodeId($row->barcodeid)
			->setSubOrderId($row->sub_orderid)
			->setSendUnitId($row->send_unitid)
			->setRecUnitId($row->rec_unitid)
			->setSendCourierId($row->send_courierid)
			->setRecCourierId($row->rec_courierid)
			->setIfPayNow($row->if_pay_now)
			->setPayType($row->pay_type)
			->setFee($row->fee)
			->setSubStatus($row->sub_status)
			->setRemark($row->remark)
			->setSizeId($row->size_id)
			->setTimeCreated($row->time_created)
			->setTimeSendbox($row->time_sendbox)
			->setTimeDepart($row->time_depart)
			->setTimeArrive($row->time_arrive)
			->setTimeRecbox($row->time_recbox);
	}
	
	public function findByBarcode($barcode){
		$entries   = array();

		$table = $this->getDbTable();
		$db = $table->getAdapter();
		$where = $db->quoteInto('barcodeid = ?', $barcode);
		$resultSet = $table->fetchAll($where);
		
		foreach ($resultSet as $row) {
			$entry = new Application_Model_Subscribe();
			$entry->setId($row->id)
			->setSenderName($row->sender_name)
			->setSenderId($row->sender_id)
			->setSenderTel($row->sender_tel)
			->setSenderAddrId($row->sender_addrid)
			->setRecipient($row->recipient)
			->setRecTel($row->rec_tel)
			->setRecEmail($row->rec_email)
			->setRecAddrId($row->rec_addrid)
			->setBarcodeId($row->barcodeid)
			->setSubOrderId($row->sub_orderid)
			->setSendUnitId($row->send_unitid)
			->setRecUnitId($row->rec_unitid)
			->setSendCourierId($row->send_courierid)
			->setRecCourierId($row->rec_courierid)
			->setIfPayNow($row->if_pay_now)
			->setPayType($row->pay_type)
			->setFee($row->fee)
			->setSubStatus($row->sub_status)
			->setRemark($row->remark)
			->setSizeId($row->size_id)
			->setTimeCreated($row->time_created)
			->setTimeSendbox($row->time_sendbox)
			->setTimeDepart($row->time_depart)
			->setTimeArrive($row->time_arrive)
			->setTimeRecbox($row->time_recbox);
			$entries[] = $entry;
		}
		
		return $entries;
	}
	
	public function fetchAll()
	{
		$resultSet = $this->getDbTable()->fetchAll();
		$entries   = array();
		foreach ($resultSet as $row) {
			$entry = new Application_Model_Subscribe();
			$entry->setId($row->id)
			->setSenderName($row->sender_name)
			->setSenderId($row->sender_id)
			->setSenderTel($row->sender_tel)
			->setSenderAddrId($row->sender_addrid)
			->setRecipient($row->recipient)
			->setRecTel($row->rec_tel)
			->setRecEmail($row->rec_email)
			->setRecAddrId($row->rec_addrid)
			->setBarcodeId($row->barcodeid)
			->setSubOrderId($row->sub_orderid)
			->setSendUnitId($row->send_unitid)
			->setRecUnitId($row->rec_unitid)
			->setSendCourierId($row->send_courierid)
			->setRecCourierId($row->rec_courierid)
			->setIfPayNow($row->if_pay_now)
			->setPayType($row->pay_type)
			->setFee($row->fee)
			->setSubStatus($row->sub_status)
			->setRemark($row->remark)
			->setSizeId($row->size_id)
			->setTimeCreated($row->time_created)
			->setTimeSendbox($row->time_sendbox)
			->setTimeDepart($row->time_depart)
			->setTimeArrive($row->time_arrive)
			->setTimeRecbox($row->time_recbox);
			$entries[] = $entry;
		}
		return $entries;
	}

}

